﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Space_Cleaner.SpaceCleaner
{
    class SC_SpaceCraft : SC_SpaceObject, SC_IKeyboardListener
    {
        private int shield = 3;

        public int Shield
        {
            get { return shield; }
            set { shield = value; }
        }

        public SC_SpaceCraft(Vector2 _position, Texture2D _texture)
            : base(_position, 2.80f, 512.0f, _texture)
        {
            this.rotationVel = 0.0f;
            this.size = 0.05f;
            this.rotationAbsorption = 0.05f;
        }

        public void controlAccelerate(float _acceleration)
        {
            Vector2 acc = new Vector2((float)Math.Cos(rotation),
                                (float)Math.Sin(rotation));

            acc.Normalize();
            acc *= _acceleration;

            addForce(acc);
        }

        public void controlRotate(float _rotation)
        {
            rotationVel += _rotation;
        }

        public override void draw(SpriteBatch _spriteBatch, SC_Display _display)
        {
            base.draw(_spriteBatch, _display);
            drawOrbit(this, _spriteBatch, _display);
        }

        public void onKRotLeft()
        {
            this.controlRotate(-0.0001f);
        }

        public void onKRotRight()
        {
            this.controlRotate(0.0001f);
        }

        public void onKAccelerate()
        {
            if (Activated)
            {
                this.controlAccelerate(-0.0001f);

                SC_Effect sceffec = new SC_Effect(SC_TextureManager.getTexture("fire"));
                SC_World.getInstance().addObject(sceffec);
                sceffec.Activated = true;
                sceffec.Position = this.Position;
                sceffec.Velocity = this.Velocity;
                sceffec.Rotation = this.Rotation + MathHelper.ToRadians(155.0f);
                sceffec.Duration = 0;
                sceffec.Size = 1.0f;
            }
        }

        public void onKBreak()
        {
            if (Activated)
            {
                this.controlAccelerate(0.0001f);
            }
        }

        public void onKMagnet()
        {
            if (Activated)
            {
                Vector2 dist;
                float length = 0;

                //addEffect(eff2, this, 5);
                SC_Effect sceffec = new SC_Effect(SC_TextureManager.getTexture("mag"));
                SC_World.getInstance().addObject(sceffec);

                sceffec.Activated = true;
                sceffec.Position = this.Position;
                sceffec.Velocity = this.Velocity;
                sceffec.Rotation = this.Rotation + MathHelper.ToRadians(155.0f);

                sceffec.Duration = 0;
                sceffec.Size = 3;


                foreach (SC_SpaceObject obj1 in SC_World.getInstance().getPhysicsObjects())
                {
                    if (obj1.GetType() != typeof(SC_Earth) && obj1 != this && obj1.GetType() != typeof(SC_Satellite))
                    {
                        dist = (this.Position - obj1.Position);
                        if (dist.Length() <= 500)
                        {
                            length = dist.Length();
                            dist.Normalize();
                            //dist *= 0.0001f;
                            dist *= (100.0f / (length * length));

                            obj1.addForce(-dist);
                        }
                    }
                }
            }
        }
    }
}
